package com.x.crm.dao;

import com.x.crm.model.Coopertype;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CoopertypeDao extends BaseDao{


	private final static String baseSelectCoopertypeListSql = "SELECT * FROM  CRM_COOPERTYPE  I " +
			"WHERE 1=1 ";

	public List<Coopertype> selectCoopertypeList(Coopertype coopertype) throws Exception {
		StringBuilder sb = new StringBuilder(baseSelectCoopertypeListSql);
		List<Object> params = new ArrayList<Object>();
		searchBasicCondition(coopertype, sb, params);
		sb.append(" ORDER BY I.CREATE_TIME DESC");
		return this.queryForList(sb.toString(), Coopertype.class, params, coopertype);
	}


	private final static String baseSelectCoopertypeNumSql = "SELECT COUNT(COOPERTYPE_ID) FROM  CRM_COOPERTYPE  I " +
			"WHERE 1=1 ";

	public int selectCoopertypeNum(Coopertype coopertype) throws Exception {
		StringBuilder sb = new StringBuilder(baseSelectCoopertypeNumSql);
		List<Object> params = new ArrayList<Object>();
		searchBasicCondition(coopertype, sb, params);
		return this.queryForInt(sb.toString(), params);
	}


	private void searchBasicCondition(Coopertype coopertype, StringBuilder sb, List<Object> params) {
		if (this.isString(coopertype.getCoopertypeId())) {
			sb.append("AND I.COOPERTYPE_ID = ? ");
			params.add(coopertype.getCoopertypeId());
		}
		if (this.isNotNull(coopertype.getCoopertypeState())) {
			sb.append("AND I.COOPERTYPE_STATE = ? ");
			params.add(coopertype.getCoopertypeState());
		}
	}


}
